package com.temp.lock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class JavaThreadMain {
    public static boolean enable = false;
    public static void main(String[] args) {
        ExecutorService executors = Executors.newFixedThreadPool(10);
        JavaThreadMain jt = new JavaThreadMain();
        for(int i=0;i<1000;i++){
            executors.submit(new Runnable() {
                @Override
                public void run() {
                    if(flag()){
                        jt.print();
                    } else {
                        jt.print22();
                    }
                }
            });
        }

    }
    public static synchronized boolean flag(){
        enable = !enable;
        return enable;
    }

    public synchronized void print(){
        System.out.println(Thread.currentThread().getId()+" print start");
        try{
            Thread.sleep(1000);
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
            System.out.println(Thread.currentThread().getId()+" print end");
        }

    }

    public synchronized void print22(){
        System.out.println(Thread.currentThread().getId()+" print2 start");
        try{
            Thread.sleep(1000);
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
            System.out.println(Thread.currentThread().getId()+" print2 end");
        }

    }
}
